import axios from 'axios'
// 配置请求的跟路径102.79.167.41
axios.defaults.baseURL = 'http://127.0.0.1/EXAM/'

// 登录界面请求方法
const userLogin = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: params,
    traditional: true,
    withCredentials: true,
    transformRequest: [
      function (data) {
        let ret = ''
        for (const it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }
    ]
  }).then(res => res.data)
}

// 通用公用方法
const getInfo = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      USERTOKEN: sessionStorage.getItem('token')
    },
    data: params,
    traditional: true,
    withCredentials: true,
    transformRequest: [
      function (data) {
        let ret = ''
        for (const it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }
    ]
  }).then(res => res.data)
}

export {
  userLogin,
  getInfo
}
